from django.utils.deprecation import MiddlewareMixin
from mymengduo.settings import SECRET_KEY
import jwt


class JWTMiddleware(MiddlewareMixin):

    def procerss_request(self,request):
        # 拦截所有的请求 对令牌进行解析 并得出当前访问的用户的身份(uid)
        token = request.GET.get("token")
        # 解析这个令牌
        try:
            data = jwt.decode(token, SECRET_KEY, algorithms=["HS256"])
            uid = data.get("uid")
        except Exception as e:
            uid = None

        # request.POST['uid'] = uid
        request.COOKIES['uid'] = uid


from django.utils.deprecation import MiddlewareMixin
from mymengduo.settings import SECRET_KEY  # 导入 setting中的 秘钥
import jwt


# class JWTMiddleware(MiddlewareMixin):
#
#     def process_request(self, request):
#         # 拦截所有的请求 对令牌进行解析 并得出当前访问的用户的身份(uid)
#         token = request.GET.get("token")
#         # 解析这个令牌
#         try:
#             data = jwt.decode(token, SECRET_KEY, algorithms=["HS256"])
#             uid = data.get("uid")
#         except Exception as e:
#             print(e)
#             uid = None
#         # request.POST["uid"] = uid
#         request.COOKIES["uid"] = uid
